import 'package:syria_sports/widgets/dynamic_widget.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:syria_sports/widgets/dynamic_widget/basic/dynamic_widget_json_exportor.dart';
import 'package:syria_sports/widgets/screen_state_widget.dart';
import '../../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import '../../../dialogs/ask_reports_dialog.dart';
import '../../../widgets/app_bar/outline_icon_button.dart';
import '../../../widgets/dynamic_widget/click_events.dart';
import 'dynamic_ui_cubit.dart';
import 'package:flutter/material.dart';

class DynamicUiScreen extends StatelessWidget {
  final String? route;
  const DynamicUiScreen({Key? key, this.route}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return Scaffold(
      appBar: _buildAppBar(context: context),
      body: BlocProvider(
        create: (context) => AdditionalOperationsCubit()..init(route: route),
        child: BlocConsumer<AdditionalOperationsCubit,
            AdditionalOperationsState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            var cubit = AdditionalOperationsCubit.get(context);

            return Container(
              height: height,
              width: width,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage(ImageConstant.imgBackground),
                  fit: BoxFit.cover,
                ),
              ),
              child: ScreenStateWidget(
                isDone: state is AdditionalOperationsDone,
                isError: state is AdditionalOperationsError,
                isLoading: state is AdditionalOperationsLoading,
                onRefresh: cubit.getUI,
                onDone: ListView(children: [
                  SizedBox(
                    height: height - 140.v,
                    width: width,
                    child: DynamicWidgetJsonExportor(
                      child: DynamicWidgetBuilder.build(
                          cubit.uiData,
                          context,
                          DefaultClickListener(
                            context: context,
                            cubit: cubit,
                          )),
                    ),
                  ),
                ]),
              ),
            );
          },
        ),
      ),
    );
  }

  PreferredSizeWidget _buildAppBar({required BuildContext context}) {
    return CustomAppBar(
      leading: OutlineIconButton(
        imagePath: ImageConstant.imgRightIcon,
        onTapped: () {
          Navigator.pop(context);
        },
      ),
      actions: [
        OutlineIconButton(
          icon: Icons.help_outline,
          margin: const EdgeInsets.all(8),
          padding: const EdgeInsets.all(2),
          onTapped: () async {
            showDialog(
                barrierDismissible: true,
                context: NavigatorService.navigatorKey.currentState!.overlay!.context,
                builder: (BuildContext context) {
                  return const AskReportsDialog();
                });
          },
        )
      ],
      title: AppbarSubtitle(text: 'المزيد'),
    );
  }
}

class DefaultClickListener extends UiListener {
  DefaultClickListener(
      {required BuildContext context, required AdditionalOperationsCubit cubit})
      : super(cubit: cubit, context: context);

  @override
  void onClicked(UiEvents? event) {
    event?.run(context: context, cubit: cubit);
  }

  @override
  void onMenuItemsChanged(UiEvents? event) {
    event?.run(context: context, cubit: cubit);
  }
}
